public class Solution238 {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        res[0] = 1;
        for (int i = 0; i < n - 1; i++) {
            res[i + 1] = res[i] * nums[i];
        }
        int mul = 1;
        for (int i = n - 1; i > 0; i--) {
            mul *= nums[i];
            res[i - 1] *= mul;
        }
        return res;
    }
}
